package com.example.jh352160.surfingtv.thematic_details.customView.adpter;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import com.example.jh352160.surfingtv.thematic_details.customView.AllCommodityItem;

import java.util.List;

/**
 * Created by jh352160 on 2016/8/1
 */

public class CommodityPagerAdapter extends android.support.v4.view.PagerAdapter {

    private Context context;
    private List<String> fromList, detilUrlList, titleList, imageUrlList, priceList;
    private List<Boolean> isLikeList;
    private List<Integer> targetIdList;
    //因为该pager可以不按顺序点击，若使用普通List可能导致View缺失，从而导致某些view并未在显示的时候被加载，所以使用稀疏数组SparseArray
    private SparseArray<View> viewArray;

//    public CommodityPagerAdapter(List<View> commdities) {
//        List<View> commdities1 = commdities;
//    }

    public CommodityPagerAdapter(Context context, List<String> fromList, List<String> detilUrlList, List<String> titleList, List<String> imageUrlList, List<String> priceList, List<Boolean> isLikeList, List<Integer> targetIdList) {
        this.fromList = fromList;
        this.detilUrlList = detilUrlList;
        this.titleList = titleList;
        this.imageUrlList = imageUrlList;
        this.priceList = priceList;
        this.isLikeList = isLikeList;
        this.targetIdList = targetIdList;
        this.context = context;
        viewArray = new SparseArray();
    }

    @Override
    public int getCount() {
        return titleList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        AllCommodityItem item = new AllCommodityItem(context);
        item.setId(position);
        item.setItem(imageUrlList.get(position), titleList.get(position), priceList.get(position));
        item.setData(fromList.get(position), detilUrlList.get(position), isLikeList.get(position), targetIdList.get(position));
        container.addView(item);
        viewArray.put(position, item);
        return item;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(viewArray.get(position));
    }
}
